package edu.nctu.csie.jichang.dp.structural;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyDynamic {
	public static void main(String[] args) {
		DynamicProxyClass proxy = new DynamicProxyClass();
		ProxyDynamicInterface tInterface = (ProxyDynamicInterface)proxy.bind(new DClass());
		tInterface.doExecute();
	}
}
interface ProxyDynamicInterface {
	void doExecute();
}
class DClass implements ProxyDynamicInterface{
	public void doExecute() {System.out.println("do execute");}
}
class DynamicProxyClass implements InvocationHandler{
	private Object delegate;
	public Object bind(Object delegate) {
		this.delegate = delegate;
		return Proxy.newProxyInstance(getClass().getClassLoader(),delegate.getClass().getInterfaces(),this);
	}
	public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
		System.out.println("Proxy execute before");
		Object out = method.invoke(delegate, args); 
		System.out.println("Proxy execute after");
		return out;
	}
}